package com.atguigu.demo;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @Author 孔德成
 * @Slogan 致敬大师，致敬未来的你
 * @date 2021/8/9
 */
public class CallableDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> task = new FutureTask<Integer>(() -> {
            System.out.println(Thread.currentThread().getName() + "----comes in");
            // 5 s 后返回计算结果
            TimeUnit.SECONDS.sleep(5);
            System.out.println(Thread.currentThread().getName() + "----comes out ");
            return 1000;
        });

        FutureTask<Integer> task2 = new FutureTask<Integer>(() -> {
            System.out.println(Thread.currentThread().getName() + "----comes in");
            // 5 s 后返回计算结果
            TimeUnit.SECONDS.sleep(2);
            System.out.println(Thread.currentThread().getName() + "----comes out ");
            return 2000;
        });

        new Thread(task, "A").start();
        new Thread(task2, "B").start();

        // 获取计算结果
        Integer a = task.get();
        Integer b = task2.get();
        System.out.println("a + b = " + (a + b));
    }
}


